package com.cherry.androidviewdemo.widget

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import com.cherry.androidviewdemo.R
import com.cherry.androidviewdemo.utils.dp2px

/**
 * @author DongMS
 * @since 2020/6/4
 */
class Loading58View @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : LinearLayout(context, attributeSet, defStyle) {

    private val shapeView: ShapeView

    private val shadowView: View

    //界面销毁后控制动画再次执行
    private var stopAnim = false

    private val translationHeight: Float

    init {

        View.inflate(context, R.layout.view_loading_58, this)
        shapeView = findViewById(R.id.shape_view)
        shadowView = findViewById(R.id.view_shadow)
        gravity = Gravity.CENTER
        orientation = VERTICAL

        translationHeight = 80f.dp2px(context)

        post {
            //等到onResume()后开始做下降动画
            startFallAnim()
        }
    }

    private fun startFallAnim() {

        if (stopAnim) {
            return
        }

        val translationYAnim = ObjectAnimator.ofFloat(shapeView, "translationY", 0f, translationHeight)
        translationYAnim.interpolator = AccelerateInterpolator()
        val scaleXAnim = ObjectAnimator.ofFloat(shadowView, "scaleX", 1f, 0.3f)

        val animatorSet = AnimatorSet()
        animatorSet.playTogether(translationYAnim, scaleXAnim)
        animatorSet.duration = ANIM_DURATION

        animatorSet.addListener(object : AnimatorListenerAdapter() {

            override fun onAnimationEnd(animation: Animator, isReverse: Boolean) {
                //下降动画结束后开始上抛动画
                startRaiseAnim()
            }
        })
        animatorSet.start()
    }

    private fun startRaiseAnim() {
        if (stopAnim) {
            return
        }

        val translationYAnim = ObjectAnimator.ofFloat(shapeView, "translationY", translationHeight, 0f)
        translationYAnim.interpolator = DecelerateInterpolator()
        val scaleXAnim = ObjectAnimator.ofFloat(shadowView, "scaleX", 0.3f, 1f)


        val animatorSet = AnimatorSet()
        animatorSet.playTogether(translationYAnim, scaleXAnim)
        animatorSet.duration = ANIM_DURATION

        animatorSet.addListener(object : AnimatorListenerAdapter() {

            override fun onAnimationEnd(animation: Animator, isReverse: Boolean) {
                //上升动画结束后开始下降动画
                startFallAnim()
            }

            override fun onAnimationStart(animation: Animator?, isReverse: Boolean) {
                //上升动画开始改变形状
                shapeView.changeShape()
                startRotationAnim()
            }
        })
        animatorSet.start()
    }

    private fun startRotationAnim() {
        //上升动画开始旋转动画
        val anim = when (shapeView.shape) {
            ShapeView.Shape.CIRCLE, ShapeView.Shape.RECT ->
                ObjectAnimator.ofFloat(shapeView, "rotation", 0f, 180f)
            ShapeView.Shape.TRIANGLE ->
                ObjectAnimator.ofFloat(shapeView, "rotation", 0f, 120f)
        }
        anim.duration = ANIM_DURATION
        anim.start()
    }

    override fun setVisibility(visibility: Int) {
        if (visibility == View.VISIBLE) {
            super.setVisibility(visibility)
        } else {
            super.setVisibility(View.INVISIBLE)
            shapeView.clearAnimation()
            shadowView.clearAnimation()
            val parent = parent as? ViewGroup
            if (parent != null) {
                parent.removeView(this)
                removeAllViews()
            }
            stopAnim = true
        }
    }


    companion object {
        const val ANIM_DURATION = 500L
    }

}